<?php
/** 
 * collection of properties and methods for the sr23 class
 */

class sr23_model {
    public $sr23a_keys = array(
        'NDB_No', 
        'Shrt_Desc',
        'Water',
        'Energ_Kcal',
        'Protein',
        'Lipid_Tot',
        'Ash',
        'Carbohydrt',
        'Fiber_TD',
        'Sugar_Tot',
        'Calcium',
        'Iron',
        'Magnesium',
        'Phosphorus',
        'Potassium',
        'Sodium',
        'Zinc',
        'Copper',
        'Manganese',
        'Selenium',
        'Vit_C',
        'Thiamin',
        'Riboflavin',
        'Niacin',
        'Panto_acid',
        'vit_B6',
        'Folate_Tot',
        'Folic_acid',
        'Food_Folate',
        'Folate_DFE',
        'Choline_Tot',
        'Vit_B12',
        'Vit_A_IU',
        'Vit_A_RAE',
        'Retinol',
        'Alpha_Carot',
        'Beta_Carot',
        'Beta_Crypt',
        'Lycopene',
        'Lut_Zea',
        'Vit_E',
        'Vit_D_mcg',
        'Vit_D_IU',
        'Vit_K',
        'FA_Sat',
        'FA_Mono',
        'FA_Poly',
        'Cholestrl',
        'GmWt_1',
        'GmWt_Desc1',
        'GmWt_2',
        'GmWt_Desc2',
        'Refuse_Pct');

    public $sr23fd_keys = array(
        'NDB_No',
        'FdGrp_Cd',
        'Long_Desc',
        'Shrt_Desc',
        'ComName',
        'ManufacName',
        'Survey',
        'Ref_desc',
        'Refuse',
        'SciName',
        'N_Factor',
        'Pro_Factor',
        'Fat_Factor',
        'CHO_Factor');

    public $dbh;   
    
    /**
     * the main PDO method, that outputs the PDO object to be used repeatedly in this application
     */
    public function __construct() {
        // $dbuser = 'letidc5_sci';
        // $dbpass = '+5ursD@Y';
        require_once "config.php";
        // $dbh = new PDO('mysql:dbname=letidc5_sr23;host=localhost', $dbuser, $dbpass);
        $this->dbh = new PDO($connection, $dbuser, $dbpass);
    }

    
    /**
     * above is end of initial set-up
     * below are methods that will be used repeatedly by the application
     */
    
    // function to output nutrient object w/o calculation
    public function get_nutrient($ndbno) {
        $query = "select * from abbreviated where NDB_No = '~$ndbno~'";      
        $sth = $this->dbh->prepare($query);
        $sth->execute();

        $result = $sth->fetchAll(PDO::FETCH_ASSOC);
        return $result[0];
    }

    // get food long description
    public function get_long_description($ndbno) {
        $query = "select Long_Desc from food_description where NDB_No = '~$ndbno~'";
        $sth = $this->dbh->prepare($query);
        $sth->execute();

        $array = $sth->fetchAll(PDO::FETCH_ASSOC);
        return str_replace('~','',$array[0]['Long_Desc']); 
    }

    // function to output nutrient object after calculation
    public function calculate_nutrient_from_weight($ndbno, $weight) {
        $nutrient = array(); // set the empty array to return

        $query = "select * from abbreviated where NDB_No = '~$ndbno~'";
        $sth = $this->dbh->prepare($query);
        $sth->execute();

        $result = $sth->fetchAll(PDO::FETCH_ASSOC);
        // die(print_r($result));
        // you want items 2 .. 47
        // for ($i = 2;$i < 48; $i++) {
        foreach ($result[0] as $k => $v) {    
            // calculate
            // VH = (N * CM) / 100
            // VH: The nutrient content per the desired common measure
            // N: The nutrient content per 100g
            // For NDB_No 01001, fat = 81.11g/100g
            // CM: grams of the common measure
            // for NDB_No 01001, 1 tablespoon = 14.2g
            // so, using this formula for the above example:
            // VH = (81.11 * 14.2) / 100 = 11.52g of fat in 1 tablespoon of butter
            if ($k != 'GmWt_1' and $k != 'GmWt_2' and $k != 'Refuse_Pct' and $k != 'Shrt_Desc' and $k != 'NDB_No') {
                $nutrient[$k] = ($v * $weight) / 100;
            }
            else
            {
                $nutrient[$k] = $v;
            }
        }
        return $nutrient;
    }

    /**
     * takes a string of desc to match against the db
     * depending on the nature of desc, the function would return either a pretty print array or string
     */
    public function match_description($desc) {
        $results = array();
        // check to see if search description has quotes, if so, search as single words, otherwise do a search for each word
        if (preg_match("/^\"/", $desc, $matches)) {
            // strip the quotes
            $desc = str_replace('"','',$desc); // get rid of quotes
            // die($desc);
            // single word search
            $query = "select NDB_No, Long_Desc from food_description where Long_Desc like '%$desc%' or Long_Desc like '%$desc' or Long_Desc like '$desc%'";   
            $sth = $this->dbh->prepare($query);
            $sth->execute();
            $results = $sth->FetchAll(PDO::FETCH_ASSOC);
            
            return $results; 
        }
        else
        {
            // multi word search
            // break apart by space     
            $words = explode(" ", $desc); // split apart by space
            // die(print_r($words));
            foreach ($words as $desc) {
                $query = "select NDB_No, Long_Desc from food_description where Long_Desc like '%$desc%' or Long_Desc like '%$desc' or Long_Desc like '$desc%'"; 
                $sth = $this->dbh->prepare($query);
                $sth->execute();
                $results[] = $sth->FetchAll(PDO::FETCH_ASSOC);
            }
            return $results;
        }
    }

    /** to output organized arrays , instead of printing out a long line of what seems at first to be gibberish */
    public function print_pretty_array($myArray) {
        print '<pre>';
        print_r($myArray);
        print '</pre>'; 
    }
}
